<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('productCenter.appConfig.helpCenter.problem')">
          <el-input v-model="searchForm.title" :placeholder="$t('common.input')" />
        </el-form-item>
        <el-form-item :label="$t('productCenter.appConfig.helpCenter.status')">
          <el-select v-model="searchForm.status" clearable style="width: 100%">
            <el-option
              v-for="item in $t('productCenter.appConfig.helpCenter.state')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="primary" :loading="addLoading" @click="add">
        {{ $t('productCenter.appConfig.helpCenter.add') }}
      </el-button>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%;margin-top: 10px"
      >
        <el-table-column
          fixed="left"
          prop="id"
          :label="tableText['id']"
          min-width="100"
        />
        <el-table-column
          fixed="left"
          prop="order"
          :label="tableText['order']"
          min-width="80"
        />
        <el-table-column
          prop="title"
          :label="tableText['problem']"
          min-width="170"
        />
        <el-table-column
          prop="modifyTime"
          :label="tableText['updateTime']"
          min-width="170"
        />
        <el-table-column
          prop="content"
          :label="tableText['content']"
          min-width="170"
        />
        <el-table-column
          prop="usefulCount"
          :label="tableText['itWorks']"
          min-width="80"
        />
        <el-table-column
          prop="uselessCount"
          :label="tableText['useless']"
          min-width="80"
        />
        <el-table-column
          prop="modifier"
          :label="tableText['updateMan']"
          min-width="100"
        />
        <el-table-column
          prop="modifyTimeStr"
          :label="tableText['updateTime']"
          min-width="120"
        />
        <el-table-column
          fixed="right"
          prop="status"
          :label="tableText['status']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ setValue(scope.row['status'], 'state') }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="300"
        >
          <template slot-scope="scope">
            <el-button size="small" :loading="editLoading" @click="editItem(scope.row)">{{ tableText['edit'] }}</el-button>
            <el-button size="small" :disabled="scope.row.order === 1" @click="moveUp(scope.row)">{{ tableText['moveUp'] }}</el-button>
            <el-button size="small" :disabled="scope.row.order === maxOrder" @click="moveDown(scope.row)">{{ tableText['moveDown'] }}</el-button>
            <el-button size="small" @click="deleteItem(scope.row)">{{ tableText['delete'] }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <help-center-dialog
      :show-dialog="editDialog"
      :choose-item="chooseItem"
      :max-order="maxOrder"
      @change-dialog="changeDialog"
      @edit-success="editSuccess"
    />
    <delete-dialog
      ref="delete-dialog"
      :delete-state="deleteState"
      :loading="deleteLoading"
      @change-delete="changeDelete"
      @delete-this="deleteThis"
    />
  </el-row>
</template>

<script>
import { getHelpCenter, getMaxOrderAddOne, deleteHelpCenter, moveUpHelpCenter, moveDownHelpCenter } from '@/api/product'
import HelpCenterDialog from '@/components/product/help-center-dialog'
import DeleteDialog from '@/components/delete/index.vue'
export default {
  name: 'HelpCenter',
  components: {
    HelpCenterDialog,
    DeleteDialog
  },
  data() {
    return {
      loading: false,
      addLoading: false,
      editLoading: false,
      editDialog: false,
      deleteState: false,
      deleteLoading: false,
      maxOrder: 1,
      tableData: [],
      tableText: {},
      totalNumber: 0,
      searchForm: this.params(),
      chooseItem: {
        id: null,
        order: null,
        status: null,
        title: null,
        content: null
      }
    }
  },
  mounted() {
    this.tableText = this.$t('productCenter.appConfig.helpCenter')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        title: null,
        status: ''
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.getData()
    },
    getMaxOrder() {
      getMaxOrderAddOne()
        .then(res => {
          if (res.success) {
            this.maxOrder = res.data - 1
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    getData() {
      this.loading = true
      getHelpCenter(this.searchForm)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.getMaxOrder()
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    setValue(val, data) {
      const name = this.$t(`productCenter.appConfig.helpCenter.${data}`).filter(item => {
        return item.value === val
      })
      return (name && name[0] && name[0].desc) ? name[0].desc : ''
    },
    add() {
      getMaxOrderAddOne()
        .then(res => {
          this.addLoading = false
          if (res.success) {
            this.maxOrder = res.data
            this.choose({
              order: this.maxOrder
            })
            this.editDialog = true
            this.addLoading = false
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.addLoading = false
          this.$message.error(err.message)
        })
    },
    editItem(item) {
      getMaxOrderAddOne()
        .then(res => {
          this.editLoading = false
          if (res.success) {
            this.maxOrder = res.data - 1
            this.choose(item)
            this.editDialog = true
            this.editLoading = false
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.editLoading = false
          this.$message.error(err.message)
        })
    },
    choose(param) {
      this.chooseItem.id = param.id
      this.chooseItem.order = param.order
      this.chooseItem.status = param.status
      this.chooseItem.title = param.title
      this.chooseItem.content = param.content
    },
    moveUp(item) {
      this.loading = true
      moveUpHelpCenter(item.id)
        .then(res => {
          if (res.success) {
            this.$message.success(this.$t('common.tip.editSuccess'))
            this.getData()
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    moveDown(item) {
      this.loading = true
      moveDownHelpCenter(item.id)
        .then(res => {
          if (res.success) {
            this.$message.success(this.$t('common.tip.editSuccess'))
            this.getData()
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    deleteItem(item) {
      this.chooseItem.id = item.id
      this.changeDelete(true)
    },
    changeDelete(param) {
      this.deleteState = param
    },
    deleteThis() {
      this.deleteLoading = true
      const id = this.chooseItem.id
      deleteHelpCenter(id)
        .then(res => {
          this.deleteLoading = false
          if (res.success) {
            this.$message.success(this.$t('common.tip.deleteSuccess'))
            this.changeDelete(false)
            this.getData()
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.deleteLoading = false
          this.$message.error(err.message)
        })
    },
    editSuccess() {
      this.getData()
    },
    changeDialog(data, state) {
      this[data] = state
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
